package com.cj.mobile.iabp.mvp.model;

import android.text.TextUtils;

import com.cj.mobile.iabp.app.global.AccountManager;
import com.cj.mobile.iabp.mvp.contract.AddSurgeryInfoContract;
import com.cj.mobile.iabp.mvp.model.api.service.AccountService;
import com.cj.mobile.iabp.mvp.model.entity.DictInfo;
import com.cj.mobile.iabp.mvp.model.entity.SurgeryPage;
import com.jess.arms.cj.ApiOperator;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 2025/09/24 15:36
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class AddSurgeryInfoModel extends BaseModel implements AddSurgeryInfoContract.Model {
    @Inject
    ApiOperator apiOperator;                                                                        // 数据转换

    @Inject
    AccountManager mAccountManager;

    @Inject
    public AddSurgeryInfoModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.apiOperator = null;
        this.mAccountManager = null;
    }

    @Override
    public Observable<List<DictInfo>> getDictList(String type) {
        Map<String, Object> params = new HashMap<>();
        params.put("type", type);

        return apiOperator.chain(params, request -> mRepositoryManager.obtainRetrofitService(AccountService.class).getDictList(mAccountManager.getToken(), request));
    }

    @Override
    public Observable<List<String>> upload(List<String> files) {

        /*图片集*/
        List<MultipartBody.Part> parts = new ArrayList<>(files.size());
        for (String val : files) {
            File file = new File(val);
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
            MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
            parts.add(part);
        }

        //用户登录id
        RequestBody userIdVal = RequestBody.create(MediaType.parse("text/plain"), mAccountManager.getUserid());
        return mRepositoryManager.obtainRetrofitService(AccountService.class).uploadImages(mAccountManager.getToken(), userIdVal, parts).flatMap(ApiOperator.<List<String>>transformResponse());
    }

    @Override
    public Observable<Double> getVis(String bloodPasur, String dba, String ssx, String qjssx, String mln, String dbf, String sys, List<SurgeryPage> data) {
        Map<String, Object> params = new HashMap<>();
        if (!TextUtils.isEmpty(bloodPasur))
            params.put("bloodPasur", bloodPasur);
        params.put("dba", dba);
        params.put("ssx", ssx);
        params.put("qjssx", qjssx);
        params.put("mln", mln);
        params.put("dbf", dbf);
        params.put("sys", sys);
        params.put("data", data);

        return apiOperator.chain(params, request -> mRepositoryManager.obtainRetrofitService(AccountService.class).getVis(mAccountManager.getToken(), request));
    }

    @Override
    public Observable<Integer> addSurgery(String per, String patienId, String doctorId, String deptId, String deptSec, String regCode,
                                          String patientName, String doctorName, List<SurgeryPage> data) {
        Map<String, Object> params = new HashMap<>();
        params.put("per", per);
        params.put("patienId", patienId);
        params.put("doctorId", doctorId);
        params.put("deptId", deptId);
        params.put("deptSec", deptSec);
        params.put("regCode", regCode);
        params.put("patientName", patientName);
        params.put("doctorName", doctorName);

        // 使用 Gson 将 List<SurgeryPage> 转换为 JSONArray 字符串
//        Gson gson = new Gson();
//        JsonArray jsonArray = new JsonArray();
//        for (SurgeryPage page : data) {
//            JsonElement jsonElement = gson.toJsonTree(page);
//            jsonArray.add(jsonElement);
//        }
//        params.put("data", jsonArray);
        params.put("data", data);

        return apiOperator.chain(params, request -> mRepositoryManager.obtainRetrofitService(AccountService.class).addSurgery(mAccountManager.getToken(), request));
    }

    @Override
    public Observable<Integer> updateSurgery(String per, String id, String patienId, String doctorId, String deptId, String deptSec, String regCode, String patientName, String doctorName, List<SurgeryPage> data) {
        Map<String, Object> params = new HashMap<>();
        params.put("per", per);
        params.put("id", id);
        params.put("patienId", patienId);
        params.put("doctorId", doctorId);
        params.put("deptId", deptId);
        params.put("deptSec", deptSec);
        params.put("regCode", regCode);
        params.put("patientName", patientName);
        params.put("doctorName", doctorName);

        // 使用 Gson 将 List<SurgeryPage> 转换为 JSONArray 字符串
//        Gson gson = new Gson();
//        JsonArray jsonArray = new JsonArray();
//        for (SurgeryPage page : data) {
//            JsonElement jsonElement = gson.toJsonTree(page);
//            jsonArray.add(jsonElement);
//        }
//        params.put("data", jsonArray);
        params.put("data", data);

        return apiOperator.chain(params, request -> mRepositoryManager.obtainRetrofitService(AccountService.class).updateSurgery(mAccountManager.getToken(), request));
    }
}